home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 014 / shell / command.c next >
C/C++ Source or Header  |  1995-03-17  |  8KB  |  401 lines

  1.  
  2. /*
  3.  * COMMAND.C
  4.  *
  5.  * Matthew Dillon, 24 Feb 1986
  6.  *
  7.  * Most of the commands are in here (the variable commands are in set.c)
  8.  *
  9.  */
  10.  
  11. #include <exec/types.h>
  12. #include <exec/exec.h>
  13. #include <libraries/dos.h>
  14. #include <libraries/dosextens.h>
  15. #ifdef LATTICE
  16. #include <lattice/fcntl.h>
  17. #else
  18. #include <fcntl.h>
  19. #endif
  20. #include "shell.h"
  21.  
  22. extern struct FileLock  *CreateDir(), *CurrentDir(), *ParentDir();
  23. extern struct FileLock  *Lock(), *DupLock();
  24. extern struct FileLock  *cdgo();
  25. extern char *AllocMem();
  26.  
  27. extern long disp_entry();
  28.  
  29. struct FileLock *Clock;
  30.  
  31. do_run(str)
  32. char *str;
  33. {
  34.    register int i, len;
  35.    register char *build, *ptr;
  36.    char from[128];
  37.    char to[128];
  38.  
  39.    from[127] = to[127] = '\0';
  40.    strcpy (from, "<*");
  41.    strcpy (to, ">*");
  42.    for (i = 1, len = 0; i < ac; ++i) {
  43.       ptr = (*av[i] == '>') ? to : ((*av[i] == '<') ? from : NULL);
  44.       if (ptr) {
  45.          strncpy (ptr, av[i], 126);
  46.          if (!ptr[1]) {
  47.             av[i] = "";
  48.             if (av[++i] == NULL)
  49.                goto rerr;
  50.             strncat (ptr, av[i], 125);
  51.          }
  52.          av[i] = "";
  53.          break;
  54.       }
  55.       len += strlen(av[i]) + 1;
  56.    }
  57.    len += strlen(av[0]) + strlen(from) + strlen(to) + 4;
  58.    build = malloc(len);
  59.    strcpy (build, av[0]);
  60.    strcat (build, " ");
  61.    strcat (build, from);
  62.    strcat (build, " ");
  63.    strcat (build, to);
  64.    for (i = 1; i < ac; ++i) {
  65.       if (*av[i]) {
  66.          strcat (build, " ");
  67.          strcat (build, av[i]);
  68.       }
  69.    }
  70.    i = Execute (build, 0, 0);
  71.    free (build);
  72.    return ((i) ? 1 : -1);
  73. rerr:
  74.    puts ("redirection syntax error");
  75.    return (-1);
  76. }
  77.  
  78. do_number()
  79. {
  80.    return (1);
  81. }
  82.  
  83. do_cat()
  84. {
  85.    FILE *fi;
  86.    int i;
  87.    char buf[256];
  88.  
  89.    for (i = 1; i < ac; ++i) {
  90.       if (fi = fopen (av[i], "r")) {
  91.          while (fgets (buf, 256, fi)) {
  92.             fputs (buf, stdout);
  93.             fflush (stdout);
  94.          }
  95.          fclose (fi);
  96.       } else {
  97.          perror (av[i]);
  98.       }
  99.    }
  100. }
  101.  
  102. do_dir(garbage, com)
  103. char *garbage;
  104. {
  105.    struct DPTR          *dp;
  106.    struct InfoData      *info;
  107.    char *name;
  108.    int i, stat;
  109.    long total = 0;
  110.  
  111.    if (ac == 1) {
  112.       ++ac;
  113.       av[1] = "";
  114.    }
  115.    for (i = 1; i < ac; ++i) {
  116.       if ((dp = dopen (av[i], &stat)) == NULL)
  117.          continue;
  118.       if (com < 0) {
  119.          info = (struct InfoData *)AllocMem(sizeof(struct InfoData), MEMF_PUBLIC);
  120.          if (Info (dp->lock, info)) {
  121.             int bpb = info->id_BytesPerBlock;
  122.             printf ("Unit:%2d  Errs:%3d  Bytes: %-7d Free: %-7d\n",
  123.                   info->id_UnitNumber,
  124.                   info->id_NumSoftErrors,
  125.                   bpb * info->id_NumBlocks,
  126.                   bpb * (info->id_NumBlocks - info->id_NumBlocksUsed));
  127.          } else {
  128.             perror (av[i]);
  129.          }
  130.          FreeMem (info, sizeof(*info));
  131.       } else {
  132.          if (stat) {
  133.             while (dnext (dp, &name, &stat))
  134.                total += disp_entry (dp->fib);
  135.          } else {
  136.             total += disp_entry(dp->fib);
  137.          }
  138.  
  139.       }
  140.       dclose (dp);
  141.    }
  142.    printf ("TOTAL: %ld\n", total);
  143.    return (1);
  144. }
  145.  
  146.  
  147. long
  148. disp_entry(fib)
  149. register struct FileInfoBlock *fib;
  150. {
  151.    char str[5];
  152.    register char *dirstr;
  153.  
  154.    str[4] = '\0';
  155.    str[0] = (fib->fib_Protection & FIBF_READ) ? '-' : 'r';
  156.    str[1] = (fib->fib_Protection & FIBF_WRITE) ? '-' : 'w';
  157.    str[2] = (fib->fib_Protection & FIBF_EXECUTE) ? '-' : 'x';
  158.    str[3] = (fib->fib_Protection & FIBF_DELETE) ? '-' : 'd';
  159.    dirstr = (fib->fib_DirEntryType < 0) ? "   " : "DIR";
  160.  
  161.    printf ("%s %6ld %s  %s\n", str, (long)fib->fib_Size, dirstr, fib->fib_FileName);
  162.    return ((long)fib->fib_Size);
  163. }
  164.  
  165.  
  166. do_quit()
  167. {
  168.    exit (1);
  169. }
  170.  
  171. do_echo(str)
  172. char *str;
  173. {
  174.    if (strcmp (av[1], "-n") == 0) {
  175.       fputs (next_word(next_word(str)), stdout);
  176.    } else {
  177.       puts (next_word(str));
  178.    }
  179.    fflush (stdout);
  180.    return (1);
  181. }
  182.  
  183.  
  184. do_source(str)
  185. char *str;
  186. {
  187.    register FILE *fi;
  188.    char buf[256];
  189.  
  190.    fi = fopen (next_word(str), "r");
  191.    if (fi == NULL) {
  192.       printf ("Cannot open %s\n", str);
  193.       return(-1);
  194.    }
  195.    ++H_stack;
  196.    while (fgets (buf, 256, fi) != NULL) {
  197.       buf[strlen(buf) - 1] = '\0';
  198.       exec_command (buf);
  199.    }
  200.    --H_stack;
  201.    fclose (fi);
  202.    return (1);
  203. }
  204.  
  205. /*
  206.  * cd.  Additionally, allow '..' to get you back one directory in the path.
  207.  */
  208.  
  209. do_cd()
  210. {
  211.    static char root[32];
  212.    register struct FileLock *lock;
  213.    register char *str, *ptr;
  214.    register int notdone;
  215.  
  216.    if (ac != 2) {
  217.       puts (root);
  218.       Clock = cdgo (root, NULL);
  219.       return (1);
  220.    }
  221.    str = av[1];
  222.    while (*str == '/') {
  223.       ++str;
  224.       if (Clock)
  225.          Clock = cdgo (NULL, Clock);
  226.    }
  227.    notdone = 1;
  228.    while (notdone) {
  229.       ptr = str;
  230.       while (*str && *str != '/' && *str != ':')
  231.          ++str;
  232.       notdone = *str;
  233.       *str++ = '\0';
  234.       if (ptr == str - 1)
  235.          continue;
  236.       if (notdone == ':') {
  237.          *(str-1) = notdone;
  238.          notdone = *str;
  239.          *str = '\0';
  240.          strcpy (root, ptr);
  241.          Clock = cdgo (root, NULL);
  242.          *str = notdone;
  243.       } else
  244.       if (strcmp (ptr, "..") == 0) {
  245.          if (Clock)
  246.             Clock = cdgo (NULL, Clock);
  247.       } else {
  248.          if ((lock = cdgo (ptr, NULL)) == NULL)
  249.             puts ("not found");
  250.          else
  251.             Clock = lock;
  252.       }
  253.    }
  254.    return (1);
  255. }
  256.  
  257.  
  258. struct FileLock *
  259. cdgo(ptr, lock)
  260. char *ptr;
  261. struct FileLock *lock;
  262. {
  263.    struct FileLock *newlock, *oldlock;
  264.  
  265.    if (lock)
  266.       newlock = ParentDir (lock);
  267.    else
  268.       newlock = Lock (ptr, ACCESS_READ);
  269.    if (newlock) {
  270.       if (oldlock = CurrentDir (newlock))
  271.          UnLock (oldlock);
  272.    }
  273.    return (newlock);
  274. }
  275.  
  276.  
  277. do_mkdir()
  278. {
  279.    register int i;
  280.    register struct FileLock *lock;
  281.  
  282.    for (i = 1; i < ac; ++i) {
  283.       if (lock = CreateDir (av[i])) {
  284.          UnLock (lock);
  285.          continue;
  286.       }
  287.       perror (av[i]);
  288.    }
  289.    return (1);
  290. }
  291.  
  292. do_mv()
  293. {
  294.    if (ac != 3) {
  295.       puts ("Rename required 2 arguments");
  296.       return (-1);
  297.    }
  298.    if (Rename (av[1], av[2]))
  299.       return (1);
  300.    perror (NULL);
  301.    return (-1);
  302. }
  303.  
  304. do_rm()
  305. {
  306.    register int i;
  307.  
  308.    for (i = 1; i < ac; ++i) {
  309.       if (!DeleteFile(av[i]))
  310.          perror (av[i]);
  311.    }
  312.    return (1);
  313. }
  314.  
  315.  
  316. do_history()
  317. {
  318.    register struct HIST *hist;
  319.    register int i = H_tail_base;
  320.  
  321.    for (hist = H_tail; hist; hist = hist->prev) {
  322.       printf ("%3d %s\n", i, hist->line);
  323.       ++i;
  324.    }
  325.    return (1);
  326. }
  327.  
  328. do_mem()
  329. {
  330.    long cfree, ffree;
  331.    extern long AvailMem();
  332.  
  333.    Forbid();
  334.    cfree = AvailMem (MEMF_CHIP);
  335.    ffree = AvailMem (MEMF_FAST);
  336.    Permit();
  337.  
  338.    if (ffree)
  339.       printf ("FAST memory:%10ld\n", ffree);
  340.    printf ("CHIP memory:%10ld\n", cfree);
  341.    printf ("Total -----:%5ld K\n", (ffree + cfree)/1024L);
  342. }
  343.  
  344. /*
  345.  * foreach var_name  ( str str str str... str ) commands
  346.  * spacing is important (unfortunetly)
  347.  *
  348.  * ac=0    1 2 3 4 5 6 7
  349.  * foreach i ( a b c ) echo $i
  350.  * foreach i ( *.c )   "echo -n "file ->";echo $i"
  351.  */
  352.  
  353. do_foreach()
  354. {
  355.    register int i, cstart, cend, old;
  356.    register char *cstr, *vname, *ptr;
  357.  
  358.    if (ac < 2) {
  359.       puts ("form: foreach i ( str str str.. ) command");
  360.       return (-1);
  361.    }
  362.    cstart = i = (*av[2] == '(') ? 3 : 2;
  363.    while (i < ac) {
  364.       if (*av[i] == ')')
  365.          break;
  366.       ++i;
  367.    }
  368.    if (i == ac) {
  369.       puts ("')' expected");
  370.       return (-1);
  371.    }
  372.    ++H_stack;
  373.    cend = i;
  374.    vname = strcpy(malloc(strlen(av[1])+1), av[1]);
  375.    cstr = compile_av (av, cend + 1, ac);
  376.    for (i = cstart; i < cend; ++i) {
  377. again:
  378.       ptr = av[i];
  379.       while (*ptr && *ptr != ' ')
  380.          ++ptr;
  381.       old = *ptr;
  382.       *ptr = '\0';
  383.       set_var (LEVEL_SET, vname, av[i]);
  384.       exec_command (cstr);
  385.       if (old) {
  386.          while (*++ptr && (*ptr == ' ' || *ptr == 9));
  387.          av[i] = ptr;
  388.          if (*ptr)
  389.             goto again;
  390.       }
  391.    }
  392.    --H_stack;
  393.    free (cstr);
  394.    unset_var (LEVEL_SET, vname);
  395.    free (vname);
  396.    return (1);
  397. }
  398.  
  399.  
  400.  
  401.